package com.zuowei.ycy.user.ui.mine;

import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.and.yzy.frame.util.RetrofitUtils;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.config.UserManger;
import com.zuowei.ycy.user.http.Member;
import com.zuowei.ycy.user.http.RegisterLog;
import com.zuowei.ycy.ui.BaseToolbarAty;

import butterknife.Bind;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 验证身份
 */
public class MineUpdatePhoneFirstAty extends BaseToolbarAty {
    @Bind(R.id.tv_acount)
    TextView mTvAcount;
    @Bind(R.id.et_yanzhen)
    EditText mEtYanzhen;
    @Bind(R.id.tv_yanzhenma)
    TextView mTvYanzhenma;
    MyCount mMyCount;

    @Override
    public int getLayoutId() {
        return R.layout.mine_update_phone_fitst_layout;
    }

    @Override
    public void initData() {

        mToolbar.setTitle("验证身份");
        mTvAcount.setText(UserManger.getUserInfo().getAccount());

    }

    @Override
    public void requestData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mMyCount != null) {
            mMyCount.cancel();
        }
    }

    @OnClick({R.id.tv_commit, R.id.tv_yanzhenma})
    @Override
    public void btnClick(View view) {
        super.btnClick(view);
        switch (view.getId()) {
            case R.id.tv_commit:

                if (mEtYanzhen.getText().toString().trim().length() < 4) {
                    showToast("验证码长度不正确");
                    mEtYanzhen.requestFocus();
                    return;
                }


                showLoadingDialog("正在验证");
                doHttp(RetrofitUtils.createApi(Member.class).modifyAccountOne(UserManger.getM_id(), UserManger.getUserInfo().getAccount(), mEtYanzhen.getText().toString().trim()), 2);


                break;
            case R.id.tv_yanzhenma:

                showLoadingDialog("正在发送");

                doHttp(RetrofitUtils.createApi(RegisterLog.class).sendVerify(UserManger.getUserInfo().getAccount(), "bind"), 1);


                break;
        }
    }

    private class MyCount extends CountDownTimer {

        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (mTvYanzhenma != null) {
                mTvYanzhenma.setEnabled(false);
                mTvYanzhenma.setText(millisUntilFinished / 1000 + "  s");
            }

        }

        @Override
        public void onFinish() {
            if (mTvYanzhenma != null) {
                mTvYanzhenma.setEnabled(true);
                mTvYanzhenma.setText("获取验证码");
            }

        }
    }


    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {

        switch (what) {
            case 1:
                if (mMyCount == null) {
                    mMyCount = new MyCount(60000, 1000);
                }
                mMyCount.start();
                showToast("发送成功");

                break;
            case 2:

                showToast("验证成功");
                startActivityForResult(MineUpdatePhoneAty.class, null, 1);


                break;

        }


        super.onSuccess(result, call, response, what);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
